package com.last.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.last.entity.*;
import com.last.mapper.MerchantMapper;
import com.last.service.IMerchantService;
import freemarker.template.utility.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class MerchantService extends ServiceImpl<MerchantMapper, Merchant> implements IMerchantService {

    @Autowired
    MerchantMapper merchantMapper;

    @Override
    public PageResult<Merchant> findAllByPage(PageDTO<Merchant> pageDTO) {

        IPage<Merchant> iPage = new Page<>(pageDTO.getPage(),pageDTO.getLimit());
        Merchant whereEntity = pageDTO.getData();

        QueryWrapper<Merchant> queryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(whereEntity.getUsername())){
            queryWrapper.like("username",whereEntity.getUsername());
        }

        IPage<Merchant> pageInfo = merchantMapper.selectPage(iPage,queryWrapper);

        PageResult<Merchant> pageResult = new PageResult<>();
        if(pageInfo.getRecords() == null){
            return new PageResult<Merchant>(){{setSuccess(false);setMsg("暂无数据");}};
        }else {
            pageResult.setData(pageInfo.getRecords());
            pageResult.setTotal(pageInfo.getTotal());
            pageResult.setPages(pageInfo.getPages());
            pageResult.setSuccess(true);
        }

        return pageResult;
    }

    @Override
    public Result<Merchant> resumeOrCancel(Merchant merchant) {
        if(merchant.getStatus() == 1){
            merchant.setStatus(2);
        }else{
            merchant.setStatus(1);
        }

        int b = merchantMapper.updateById(merchant);

        return new Result<>(b==1,b==1?merchant.getStatus()==1?"恢复用户信息成功！":"注销用户信息成功!":"修改用户状态失败！");
    }

}
